﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Net;
using System.IO;


public partial class Proyectos : System.Web.UI.Page
{
    DataTable workTable = new DataTable("TPerfiles");
    protected void Page_Load(object sender, EventArgs e)
    {
        creartabla();
    }
    private void creartabla()
    {
        workTable.Columns.Add("cod_per", typeof(Int32));
        workTable.Columns.Add("nom_per", typeof(String));
        workTable.Columns.Add("cod_gra", typeof(Int32));
        workTable.Columns.Add("nom_gra", typeof(String));
        workTable.Columns.Add("canti", typeof(Int32));
    }
    protected void bus_Click(object sender, EventArgs e)
    {
        ServiceCliente.cliente objcliente = new ServiceCliente.cliente();
        ServiceCliente.ClienteClient objServiceCliente = new ServiceCliente.ClienteClient();
        objcliente = objServiceCliente.ObtenerCliente(Convert.ToInt32(txtcod.Text));
        txtcli.Text = string.Empty;
        if (objcliente != null)
        {
            txtcli.Text = objcliente.nom_cli;
        }
        else
        {
            string strErrorDesc = "No existe cliente";
            Response.Write("<script language='javascript'>alert('" + strErrorDesc + "');</script>");
        }
    }

    protected void cbper_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void btnagr_Click(object sender, EventArgs e)
    {

            if (Session["Tabla"] != null)
            {
                workTable = (DataTable)Session["Tabla"];
            }
            DataRow newrow = workTable.NewRow();
            newrow["cod_per"] = cbper.SelectedItem.Value;
            newrow["nom_per"] = cbper.SelectedItem.Text;
            newrow["cod_gra"] = cbgra.SelectedItem.Value;
            newrow["nom_gra"] = cbgra.SelectedItem.Text;
            newrow["canti"] = Convert.ToInt32(txtcan.Text);

            workTable.Rows.Add(newrow);
            newrow = null;
            Session["Tabla"] = workTable;
            dgvper.DataSource = workTable;
            dgvper.DataBind();

            txtcan.Text = "";
    }

    protected void btneli_Click(object sender, EventArgs e)
    {
    }

    protected void btngenerar_Click(object sender, EventArgs e)
    {
        ServicePerfil.perfil[] objListaTodos;
        ServicePerfil.PerfilClient objServicePerfil = new ServicePerfil.PerfilClient();
        objListaTodos = objServicePerfil.ListarTodos();
        cbper.DataSource = objListaTodos;
        cbper.DataValueField = "cod_per";
        cbper.DataTextField = "nom_per";
        cbper.DataBind();

        ServiceGrado.grado[] objListaGrados;
        ServiceGrado.GradoClient objServiceGrado = new ServiceGrado.GradoClient();
        objListaGrados = objServiceGrado.ListarTodos();
        cbgra.DataSource = objListaGrados;
        cbgra.DataValueField = "cod_gra";
        cbgra.DataTextField = "nom_gra";
        cbgra.DataBind();
    }
    protected void btngua_Click(object sender, EventArgs e)
    {

    }
    protected void btnguardar_Click(object sender, EventArgs e)
    {

        string postdata = "{\"can_det\":\"4\",\"cod_cli\":\"1\",\"cod_det\":\"6\",\"cod_gra\":\"1\",\"cod_per\":\"1\",\"cod_pro\":\"1\"}";
        byte[] data = Encoding.UTF8.GetBytes(postdata);
        HttpWebRequest req = (HttpWebRequest)WebRequest
            .Create("http://localhost:3580/Detalle_Proyectos.svc/Detalle_Proyectos");
        req.Method = "POST";
        req.ContentLength = data.Length;
        req.ContentType = "application/json";
        var reqStream = req.GetRequestStream();
        reqStream.Write(data, 0, data.Length);
        var res = (HttpWebResponse)req.GetResponse();
        StreamReader reader = new StreamReader(res.GetResponseStream());
        string detalleJson = reader.ReadToEnd();

    }
}